package com.cote.douhahou.pretloan.any.data;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcAppsTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcBtyTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendlogger.AkomapalendASLger;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcHadwTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcSsTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcStorTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendASDevcWfiTsk;
import com.cote.douhahou.pretloan.any.data.akomapalendtask.AkomapalendOnDevcTskCmple;

import java.util.concurrent.CompletableFuture;

public class AkomapalendASSycManager {

    private final static String TAG = "ASSycManager";

    private AkomapalendASSycManager() {
    }

    @SuppressLint("StaticFieldLeak")
    private static AkomapalendASSycManager instance_akomapalend;

    public static AkomapalendASSycManager instance() {
        if (instance_akomapalend == null) {
            synchronized (AkomapalendASSycManager.class) {
                instance_akomapalend = new AkomapalendASSycManager();
            }
        }
        return instance_akomapalend;
    }

    public Context mContext_akomapalend;

    private AkomapalendOnDevcTskCmple mOnDevcTskCmpleakomapalend_akomapalend;

    public void init(Context applicationContext) {
        mContext_akomapalend = applicationContext;
    }

    public void sycData(AkomapalendOnDevcTskCmple completeListener) {
        try {
            this.mOnDevcTskCmpleakomapalend_akomapalend = completeListener;

            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new AkomapalendASDevcBtyTsk(mContext_akomapalend, AkomapalendASBuilder.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new AkomapalendASDevcStorTsk(mContext_akomapalend, AkomapalendASBuilder.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new AkomapalendASDevcAppsTsk(mContext_akomapalend, AkomapalendASBuilder.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new AkomapalendASDevcWfiTsk(mContext_akomapalend, AkomapalendASBuilder.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new AkomapalendASDevcHadwTsk(mContext_akomapalend, AkomapalendASBuilder.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new AkomapalendASDevcTsk(mContext_akomapalend, AkomapalendASBuilder.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(new AkomapalendASDevcSsTsk(mContext_akomapalend, AkomapalendASBuilder.T_MAG));

            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task);

            allOfFuture.thenRun(() -> {
                AkomapalendASLger.logd(TAG, "sycData: Callback after all task");
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (mOnDevcTskCmpleakomapalend_akomapalend != null) {
                        mOnDevcTskCmpleakomapalend_akomapalend.oCompleted();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
